package com.longrise.android.byjk.plugins.course.coursedetail;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.CourseTrainDirBean;
import com.longrise.android.byjk.plugins.course.pay.BatchPaymentActivity;
import com.longrise.android.byjk.plugins.course.pay.PaymentActivity;
import com.longrise.android.byjk.utils.TimeHelper;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.eventbean.PayEventBean;
import com.longrise.common.utils.AppUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * Created by callxkj on 2017/11/21.
 * 单个购买的确认订单 页面
 */

public class ConfirmSingleOrderActivity extends BaseActivity2 implements View.OnClickListener {


    private ImageView mIv;
    private TextView mTvtitle;
    private TextView mTvteacher;
    private TextView mTvtime;
    public static final String COURSE_BEAN = "course_bean";
    public static final String PRICE = "price";
    public static final String COURSE_ID = "courseid";
    private String mCourseId;
    private CourseTrainDirBean.Result mCourseBean;
    private String mPrice;
    private TextView mTvbuy;
    private TextView mTvPrice;
    private Button mBtbuy;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_confirmorder;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        setToolbarTitle("订单确认");
        mIv = (ImageView) findViewById(R.id.confirmorder_iv);
        mTvtitle = (TextView) findViewById(R.id.confirmorder_tv1);
        mTvteacher = (TextView) findViewById(R.id.confirmorder_tv2);
        mTvtime = (TextView) findViewById(R.id.confirmorder_tv3);
        mTvbuy = (TextView) findViewById(R.id.confirmorder_buy_tv);
        mTvPrice = (TextView) findViewById(R.id.confirmorder_tv_price);
        mBtbuy = (Button) findViewById(R.id.confirmorder_buy_bt);

        getIntentData();
        initData();
        initEvent();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    private void getIntentData() {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        mCourseId = extras.getString(COURSE_ID);
        mCourseBean = (CourseTrainDirBean.Result) extras.getSerializable(COURSE_BEAN);
        mPrice = intent.getStringExtra(PRICE);
    }

    private void initData() {
        String name = mCourseBean.name;
        String teachername = mCourseBean.teachername;
        String grouptime = mCourseBean.grouptime;
        String resulttime = TimeHelper.formateTime(grouptime);
        String realpic = mCourseBean.realpic;

        mTvtitle.setText(name);
        mTvteacher.setText("讲师：" + teachername);
        mTvtime.setText("课程时长：" + resulttime);
        mTvbuy.setText("¥" + mPrice);
        mTvPrice.setText("¥" + mPrice);
        Glide.with(this)
                .load(realpic)
                .placeholder(AppUtil.getResources().getDrawable(R.drawable.img03))
                .error(AppUtil.getResources().getDrawable(R.drawable.img03))
                .into(mIv);
    }

    private void initEvent() {
        mBtbuy.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirmorder_buy_bt:
                toConfirm();
                break;
        }
    }

    /**
     * 确认订单
     */
    private void toConfirm() {
        ArrayList<String> courids = new ArrayList<String>();
        courids.add(mCourseId);
        Bundle bundle = new Bundle();
        bundle.putInt(BatchPaymentActivity.BUY_TYPE, 1); ////购买途径 1-单个购买 2-批量购买 3- 团购
        bundle.putStringArrayList(BatchPaymentActivity.COURSEID_LIST, courids);
        bundle.putString(BatchPaymentActivity.COURSEPRICE, "¥"+mPrice);
        startActivity(BatchPaymentActivity.class, bundle);
    }

    /**
     * 支付成功后关闭页面
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getPayResult(PayEventBean bean){
        if(bean.isClosePage()){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
